package com.ngc.mall.controller;

import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.LotteryDomain;
import com.ngc.mall.domain.LotteryRecordDomain;
import com.ngc.mall.dto.LotteryDTO;
import com.ngc.mall.dto.LotteryItemDTO;
import com.ngc.mall.service.ILotteryItemService;
import com.ngc.mall.service.ILotteryRecordService;
import com.ngc.mall.service.ILotteryService;
import com.ngc.mall.vo.LotteryVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 抽奖活动控制类
 * @author: chenxiaolin
 * @date 2023/10/16 13:57
**/
@RestController
@RequestMapping("/lottery")
public class LotteryController {

    @Autowired
    private ILotteryService iLotteryService;

    @Autowired
    private ILotteryRecordService iLotteryRecordService;

    @Autowired
    private ILotteryItemService iLotteryItemService;

    @GetMapping("/list")
    @ApiOperation("获取所有抽奖活动")
    public ResponseResult<List<LotteryDomain>> findAllLottery(){
        return ResponseResult.success(iLotteryService.selectAll());
    }

    @GetMapping("/id/{id}")
    @ApiOperation("根据id获取抽奖活动")
    public ResponseResult<LotteryVO> findById(@PathVariable Long id){
        return ResponseResult.success(iLotteryService.findById(id));
    }

    @GetMapping("/record/list")
    @ApiOperation("获取所有中奖记录")
    public ResponseResult<List<LotteryRecordDomain>> findAllLotteryRecord(){
        return ResponseResult.success(iLotteryRecordService.selectAll());
    }

    @PostMapping("")
    @ApiOperation("创建抽奖活动")
    public ResponseResult<Long> createLottery(@RequestBody LotteryDTO lotteryDTO){
        return ResponseResult.success(iLotteryService.create(lotteryDTO));
    }

    @PutMapping("/id/{id}")
    @ApiOperation("编辑抽奖活动")
    public ResponseResult<Boolean> updateLotteryById(@PathVariable Long id,@RequestBody LotteryDomain domain){
        return ResponseResult.auto(iLotteryService.updateById(id,domain));
    }

    @GetMapping("/my/record/list")
    @ApiOperation("获取我的所有中奖记录")
    public ResponseResult<List<LotteryRecordDomain>> findMyLotteryRecord(){
        Long uid= LocalUser.getUser().getId();
        return ResponseResult.success(iLotteryRecordService.findMyLotteryRecords(uid));
    }

    @GetMapping("/record/by_lottery/{lotteryId}")
    @ApiOperation("根据活动id获取中奖记录")
    public ResponseResult<List<LotteryRecordDomain>> findMyLotteryRecord(@PathVariable Long lotteryId){
        return ResponseResult.success(iLotteryRecordService.findByLotteryId(lotteryId));
    }

    @PutMapping("/item/id/{lotteryItemId}")
    @ApiOperation("根据奖项id更新奖项")
    public ResponseResult<Boolean> updateLotteryItemById(@PathVariable Long lotteryItemId, @RequestBody LotteryItemDTO lotteryItemDTO){
        return ResponseResult.auto(iLotteryItemService.updateById(lotteryItemId,lotteryItemDTO));
    }

    @DeleteMapping("/item/id/{lotteryItemId}")
    @ApiOperation("根据奖项id删除奖项")
    public ResponseResult<Boolean> deleteLotteryItemById(@PathVariable Long lotteryItemId){
            return ResponseResult.auto(iLotteryItemService.removeById(lotteryItemId));
    }
}
